import java.util.Random;

public class Thread_count1 {
    static int Max = 1000_0000;

    static long count = 0;
    static long count1 = 0;
    static long count2 = 0;
    public static void main(String[] args) throws InterruptedException {
        //数组初始化
        int[] array = new int[Max];
        Random random = new Random();
        for (int i = 0; i < Max; i++) {
            array[i] = random.nextInt(100) + 1;
        }
        Thread t1 = new Thread(() -> {
            //偶数
            synchronized (Thread_count.class) {
                for (int i = 0; i < Max; i+= 2) {
                    count1 +=array[i];
                }
            }
        });
        Thread t2 = new Thread(() -> {
            //奇数
            synchronized (Thread_count.class) {
                for (int i = 1; i < Max; i+= 2) {
                    count2 +=array[i];
                }
            }
        });
        long sTime1 = System.currentTimeMillis();

        t1.start();
        t2.start();
        //等待线程执行完毕
        t1.join();
        t2.join();
        long eTime1 = System.currentTimeMillis();
        System.out.println("count:" + (count1+count2));
        System.out.println("多线程时间为：" + (eTime1 - sTime1));
        //测试结果是否正确
        int tmp = 0;
        long sTime2 = System.currentTimeMillis();
        for (int i = 0; i < Max; i++) {
            tmp += array[i];
        }
        long eTime2 = System.currentTimeMillis();
        System.out.println(tmp);
        System.out.println("时间为：" + (eTime2 - sTime2));
    }
}
